package vn.aptech.leaveManagementSystem.view.general;

import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import vn.aptech.leaveManagementSystem.business.dto.Employee;
import vn.aptech.leaveManagementSystem.business.dto.UserLoginInfo;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DemoChangePassword extends JFrame{
	private JPasswordField pwdOld;
	private JPasswordField pwdNew;
	private JPasswordField pwdConfirm;
	private JButton btnChange;
	private int idEmpParameter;
	private MyModel mymodel=new MyModel();
	private JButton btnCancel;
	private DemoChangePassword thisForm=this;
	public DemoChangePassword(int idEmp) {
		idEmpParameter=idEmp;
		initGUI();
		initData();
		initAction();
	}

	private void initAction() {
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					ResultSet rs= mymodel.employees.getEmployee(idEmpParameter);
					if (rs.next())
					{
						Employee tempEmp=MyParse.toEmployee(rs);
						String truePass=tempEmp.getPassword();
						String OldPass=new String(pwdOld.getPassword());
						if (truePass.equals(OldPass))
						{
							String newPass=new String(pwdNew.getPassword());
							String confirmPass=new String(pwdConfirm.getPassword());
							if (newPass.equals(confirmPass))
							{
								mymodel.employees.updateEmployeePassword(idEmpParameter, newPass);
								JOptionPane.showMessageDialog(thisForm, "Change password  successful");
								Employee owner=UserLoginInfo.getUserInfo();
								owner.setPassword(newPass);
								UserLoginInfo.setUserInfo(owner);
								close();
							}
							else
							{
								JOptionPane.showMessageDialog(thisForm, "New password and confirm new password don't match");
							}
						}
						else
						{
							JOptionPane.showMessageDialog(thisForm, "The old password is not correct");
						}
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
	}

	private void initData() {
		
	}

	private void initGUI() {
		setBounds(100, 100, 388, 214);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		
		setTitle("Change Password");
		getContentPane().setLayout(null);
		
		JLabel lblOldPassword = new JLabel("Enter old password:");
		lblOldPassword.setBounds(10, 30, 153, 14);
		getContentPane().add(lblOldPassword);
		
		JLabel lblNewPassword = new JLabel("Enter new password:");
		lblNewPassword.setBounds(10, 64, 153, 14);
		getContentPane().add(lblNewPassword);
		
		JLabel lblConfirm = new JLabel("Confirm new password:");
		lblConfirm.setBounds(10, 100, 153, 14);
		getContentPane().add(lblConfirm);
		
		pwdOld = new JPasswordField();
		pwdOld.setBounds(164, 24, 206, 20);
		getContentPane().add(pwdOld);
		
		pwdNew = new JPasswordField();
		pwdNew.setBounds(164, 61, 206, 20);
		getContentPane().add(pwdNew);
		
		pwdConfirm = new JPasswordField();
		pwdConfirm.setBounds(164, 94, 206, 20);
		getContentPane().add(pwdConfirm);
		
		btnChange = new JButton("Change");
		btnChange.setIcon(new ImageIcon("icons/gif/16x16/Modify.gif"));
		btnChange.setBounds(80, 134, 104, 23);
		getContentPane().add(btnChange);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		btnCancel.setBounds(194, 134, 104, 23);
		getContentPane().add(btnCancel);
	}
	
	public void close()
	{
		toBack();
		dispose();
	}
}
